package com.jingogo.controller;

import com.jingogo.common.Result;
import com.jingogo.service.ImageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;

@Slf4j
@RestController
@RequestMapping("/image")
@Tag(name = "图片存储", description = "图片上传与管理接口")
public class ImageController {
    @Autowired
    private ImageService imageService;

    @PostMapping("/upload")
    @Operation(summary = "上传图片", description = "上传图片")
    @PreAuthorize("hasRole('USER')")
    public Result<String> uploadImage(@RequestParam("file") MultipartFile file) {
        String url = imageService.uploadImage(file, null);
        return Result.success(url);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除图片", description = "删除图片")
    @PreAuthorize("hasRole('USER')")
    public Result<String> deleteImage(@RequestParam @NotNull Long imageId) {
        imageService.deleteImage(imageId);
        return Result.success("图片删除成功");
    }

    @PostMapping("/associate")
    @Operation(summary = "图片关联业务", description = "图片与业务数据关联")
    @PreAuthorize("hasRole('USER')")
    public Result<String> associateImage(@RequestParam @NotNull Long imageId, @RequestParam @NotNull String bizType, @RequestParam @NotNull Long bizId) {
        imageService.associateImage(imageId, bizType, bizId);
        return Result.success("图片关联成功");
    }
} 